package com.qf.service.impl;

import com.qf.constant.AppConstant;
import com.qf.entity.DevUser;
import com.qf.enums.AppEnum;
import com.qf.enums.DevStateEnum;
import com.qf.exception.AppException;
import com.qf.mapper.DevUserMapper;
import com.qf.service.DevUserService;
import com.qf.service.SendMailService;
import com.qf.util.Base64Utils;
import com.qf.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Slf4j
public class DevUserServiceImpl implements DevUserService {

    @Autowired
    private DevUserMapper devUserMapper;
    @Autowired
    private SendMailService sendMailService;

    @Override
    public DevUser findByUsername(String username) {
        DevUser devUser = new DevUser();
        devUser.setDevUsername(username);

        return devUserMapper.selectOne(devUser);
    }

    @Override
    public void checkUsername(String devName) {
        DevUser devUser = new DevUser();
        devUser.setDevUsername(devName);
        int count = devUserMapper.selectCount(devUser);
        if (count!=0){
            log.info("【用户注册】 异步校验用户名失败！devName={}",devName);
            throw new AppException(AppEnum.REGISTER_USERNAME_ERROR);
        }
    }

    @Override
    @Transactional
    public void register(DevUser devUser) {

        //封装数据   加盐，加密，默认状态
        String salt = RandomUtil.getRandom();

        String pwd = new Md5Hash(devUser.getDevPassword(), salt, AppConstant.HASH_ITERATIONS).toString();

        String code = RandomUtil.getRandom();

        devUser.setDevPassword(pwd);
        devUser.setDevCode(code);
        devUser.setDevSalt(salt);
        devUser.setDevState(DevStateEnum.not_activation.getCode());

        //执行添加
        int count = devUserMapper.insertSelective(devUser);
        if (count!=1){
            log.error("【用户注册】 用户注册失败！devUser={}",devUser);
            throw new AppException(AppEnum.REGISTER_DEVUSER_ERROR);
        }

        //发送邮件
        sendMailService.sendMail(devUser);

    }

    @Override
    @Transactional
    public void activeEmail(String email, String code) {
        String devEmail = Base64Utils.decode(email);
        String devCode = Base64Utils.decode(code);

        DevUser devUser = new DevUser();
        devUser.setDevEmail(devEmail);
        devUser.setDevCode(devCode);

        DevUser oldDevUser = devUserMapper.selectOne(devUser);

        if (oldDevUser==null){
            log.error("【用户注册】账号激活失败！链接无法识别! devUser={}",devUser);
            throw new AppException(AppEnum.ACTIVE_USER_ERROR.getCode(),"链接无法识别，请重新激活！");
        }

        if (oldDevUser.getDevState()==DevStateEnum.activation.getCode()){
            log.error("【用户注册】账号激活失败！该账号已经激活过。 devUser={}",devUser);
            throw new AppException(AppEnum.ACTIVE_USER_ERROR.getCode(),"该账号已经激活过，请直接登录！");
        }

        DevUser newDevUser = new DevUser();
        newDevUser.setId(oldDevUser.getId());
        newDevUser.setDevState(DevStateEnum.activation.getCode());

        int count = devUserMapper.updateByPrimaryKeySelective(newDevUser);
        if (count!=1){
            log.error("【用户注册】 账号激活失败！newDevUser={}",newDevUser);
            throw new AppException(AppEnum.ACTIVE_USER_ERROR);
        }


    }

    @Override
    public void nextActiveEmail(String devUsername, String devEmail) {

        DevUser devUser = new DevUser();
        devUser.setDevUsername(devUsername);
        devUser.setDevEmail(devEmail);

        DevUser oldDevUser = devUserMapper.selectOne(devUser);

        if (oldDevUser==null){
            log.error("【用户注册】账号激活失败！用户名与邮箱不匹配！ devUser={}",devUser);
            throw new AppException(AppEnum.ACTIVE_USER_ERROR.getCode(),"用户名与邮箱不匹配！请重新输入！");
        }

        if (oldDevUser.getDevState()==DevStateEnum.activation.getCode()){
            log.error("【用户注册】账号激活失败！该账号已经激活过。 devUser={}",devUser);
            throw new AppException(AppEnum.ACTIVE_USER_ERROR.getCode(),"该账号已经激活过，请直接登录！");
        }

        //发送邮件
        sendMailService.sendMail(oldDevUser);
    }

    @Override
    public List<Integer> findNotActiveOrderThreeDays() {
        return devUserMapper.findNotActiveOrderThreeDays();
    }

    @Override
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        Example example = new Example(DevUser.class);
        Example.Criteria c = example.createCriteria();
        c.andIn("id",ids);

        int count = devUserMapper.deleteByExample(example);
    }

}
